﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;

namespace BSCMFF.Web.Pages.Reporting
{
    public partial class EvaluationStatistics : BSCMFF.Core.MultiLanguagePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FillGridCategories(EvalSelection.PublicSelectedValue);
            FillGridCompetencies(EvalSelection.PublicSelectedValue);
        }

        private void FillGridCompetencies(int evalID)
        {

            BSCEntities context = new BSCEntities();

            var data = (from ev in context.Evaluation_User_Question_Value
                        join eq in context.Evaluation_Question on ev.Evaluation_Question_FK equals eq.ID
                        join ec in context.Evaluation_Category on eq.Evaluation_Category_FK equals ec.ID
                        where ec.Evaluation_FK == evalID
                        group ev by new { ev.Evaluation_Question_FK } into g
                        join eqq in context.Evaluation_Question on g.Key.Evaluation_Question_FK equals eqq.ID
                        orderby eqq.ID //descending
                        select new
                        {
                            questionID = g.Key.Evaluation_Question_FK,
                            question = eqq.Question,
                            //qlength = eqq.Question.Length,
                            perc = (int)(g.Average(ev => ev.Value - ev.Evaluation_Question.Evaluation_Category.Min) * 100 / (g.Average(ev => ev.Evaluation_Question.Evaluation_Category.Max - ev.Evaluation_Question.Evaluation_Category.Min)))
                            //perc = (g.Sum(qv => qv.Value) - g.Sum(ec => ec.Evaluation_Question.Evaluation_Category.Min)) * 100 / (g.Sum(ec => ec.Evaluation_Question.Evaluation_Category.Max) - g.Sum(ec => ec.Evaluation_Question.Evaluation_Category.Min))
                        });
            RadGridCompetencies.DataSource = data;
            RadGridCompetencies.DataBind();

        }

        private void FillGridCategories(int evalID)
        {
            BSCEntities context = new BSCEntities();

            var data = (from ec in context.Evaluation_Category
                        join eq in context.Evaluation_Question on ec.ID equals eq.Evaluation_Category_FK
                        join ev in context.Evaluation_User_Question_Value on eq.ID equals ev.Evaluation_Question_FK
                        where ec.Evaluation_FK == evalID
                        group ev by new { ec.ID } into g
                        join ecc in context.Evaluation_Category on g.Key.ID equals ecc.ID
                        orderby ecc.ID //descending
                        select new
                        {
                            categoryName = ecc.Name,
                            perc = //(g.Sum(x => x.Value - x.Evaluation_Question.Evaluation_Category.Min)
                                   (int)(g.Average(ev => ev.Value - ev.Evaluation_Question.Evaluation_Category.Min) * 100 / (g.Average(ev => ev.Evaluation_Question.Evaluation_Category.Max - ev.Evaluation_Question.Evaluation_Category.Min)))
                        });

            RadGridCategories.DataSource = data;
            RadGridCategories.DataBind();
        }
    }
}